package com.hui.service;

import com.hui.entity.ResourceCategory;
import com.hui.mapper.ResourceCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/08/15:37
 * @Description:
 */
@Service("resourceCategoryService")
public class ResourceCategoryServiceImpl implements ResourceCategoryService {
    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;

    @Override
    public List<ResourceCategory> getAllResourceCategory() {
        return resourceCategoryMapper.getAllResourceCategory();
    }

    @Override
    public void saveCategory(ResourceCategory category) {
        // 补全信息
        Date date = new Date();
        category.setCreatedTime(date);
        category.setUpdatedTime(date);
        category.setCreatedBy("system");
        category.setUpdatedBy("system");
        // 调用dao层
        resourceCategoryMapper.saveCategory(category);
    }

    @Override
    public void updateCategory(ResourceCategory category) {
        // 补全信息
        category.setUpdatedTime(new Date());
        category.setUpdatedBy("system");
        // 调用dao层
        resourceCategoryMapper.updateCategory(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        // 删除资源分类
        resourceCategoryMapper.deleteCategory(id);
        // 根据资源分类id获取该分类下的所有资源ids
        List<Integer> ids = resourceCategoryMapper.getResourceIdsByCategoryId(id);
        if(ids!=null){
            // 根据资源id删除当前资源与角色的对应关系
            for (Integer resourceId : ids) {
                resourceCategoryMapper.deleteRoleResourceRelation(resourceId);
            }
            // 根据资源分类id删除该分类下的资源
            resourceCategoryMapper.deleteResourceByCategoryId(id);
        }
    }
}
